﻿using AnesSystem.Models;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls.CommonTables.SC.DCYY
{
    /// <summary>
    /// ShuHouFangShiCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ShuHouFangShiCtrl : CustomCommonTableCtrl
    {
        private Dictionary<string, PatientVitalSignsModel> pVitalSignMap;
        List<string> vitalSignCodeList;
        public ShuHouFangShiCtrl()
        {
            InitializeComponent();
            pVitalSignMap = new Dictionary<string, PatientVitalSignsModel>();
            vitalSignCodeList = GetVitalSignList();
            if (DataManager.Instance.CurrentPatientLink != null)
            {
                sssj.Text = DataManager.Instance.CurrentPatientLink.Status.InRoomTime.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);
            }
            IniteRadioBation();
            GetVitalSigns();
        }

        private void rq_GotFocus(object sender, RoutedEventArgs e)
        {
            CustomTextCtrl custext = sender as CustomTextCtrl;
            CusDatePickerWin cusDatePicker = new CusDatePickerWin();
            cusDatePicker.SelectedDateTime = DateTime.Now;

            cusDatePicker.ShowDialog();

            if (cusDatePicker.Result)
            {
                rq.Value = cusDatePicker.SelectedDateTime.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);//2005年11月5日
            }
        }
        private void IniteRadioBation()
        {

            new RadioButtonHelper().ChangeRadioSatus(qx, ss, hm,qmzt);
            new RadioButtonHelper().ChangeRadioSatus(fb_y, fb_n);
            new RadioButtonHelper().ChangeRadioSatus(zg_y, zg_n);
            new RadioButtonHelper().ChangeRadioSatus(pcia, pcea);
            new RadioButtonHelper().ChangeRadioSatus(xg_jwsw, xg_gxb, xg_gxy);
            new RadioButtonHelper().ChangeRadioSatus(qx_qdwkn, qx_cgkn, qx_icu);
            new RadioButtonHelper().ChangeRadioSatus(jl_jwsw, jl_gxy, jl_gxb);
            new RadioButtonHelper().ChangeRadioSatus(tj_jwsw, tj_gxy, tj_gxb, tj_qt);
            new RadioButtonHelper().ChangeRadioSatus(chj_jwsw, chj_gxy, chj_qt);
        }

        private void GetVitalSigns()
        {
            pVitalSignMap.Clear();
            PatientLinkModel p = DataManager.Instance.CurrentPatientLink;
            if (Constants.AP_STATUS_MAZUIHOU.Equals(p.Application.Status) && p.Status.OutRoomTime != new DateTime())
            {
                foreach (var item in vitalSignCodeList)
                {
                    PatientVitalSignsModel PVitalSign = PatientVitalSignsServicie.Instance.GetVitalSign(p.Id.ToString(), item, CalStartTime(p.Status.OutRoomTime));
                    if (!pVitalSignMap.ContainsKey(item))
                    {
                        pVitalSignMap.Add(item, PVitalSign);
                    }
                }
                SetVitalSigns();
            }
        }

        private void SetVitalSigns()
        {
            foreach (var item in pVitalSignMap)
            {
                Console.WriteLine(item.Key + item.Value);
                switch (item.Key)
                {
                    case "param_nibp_s":
                        {
                            param_nibp_s.Value = item.Value == null ? "" : item.Value.ModValue;
                        }
                        break;
                    case "param_nibp_d":
                        {
                            param_nibp_d.Value = item.Value == null ? "" : item.Value.ModValue;
                        }
                        break;
                    case "param_HR":
                        {
                            param_HR.Value = item.Value == null ? "" : item.Value.ModValue;
                        }
                        break;
                    case "param_resp":
                        {
                            param_resp.Value = item.Value == null ? "" : item.Value.ModValue;
                        }
                        break;
                    case "param_spo2":
                        {
                            param_spo2.Value = item.Value == null ? "" : item.Value.ModValue;
                        }
                        break;
                }
            }
        }

        private List<string> GetVitalSignList()
        {
            List<string> list = new List<string>();
            list = new List<string>();
            list.Add("param_nibp_s");
            list.Add("param_nibp_d");
            list.Add("param_HR");
            list.Add("param_resp");
            list.Add("param_spo2");
            return list;
        }

        private DateTime CalStartTime(DateTime date)
        {
            int minute = date.Minute;
            int remainder = minute % 5;
            if (remainder > 0)
            {
                minute = minute - remainder;
            }
            return new DateTime(date.Year, date.Month, date.Day, date.Hour, minute, 0);
        }
    }
}
